/*
 * ZhaoBiaoGGSaxParseHtml.java
 * 招标公告的Html转换
 * 牟瑞
 * 2012-08-28
 */
package cn.com.truelore.parser;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ccil.cowan.tagsoup.Parser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import cn.com.truelore.vo.ZhaoBiaoGGVO;

public class ZhaoBiaoGGSaxParseHtml extends Parser {

	private List<ZhaoBiaoGGVO> listZhaoBiaoGG = null;

	private ZhaoBiaoGGVO itemZhaoBiaoGG = null;
	private String strTagName = "";
	private boolean flgZBGGList1_Tbl_contentTable = false;
	private boolean flgTr = false;
	private int intTdCount = 0;

	// 调用多次 开始解析
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// 初始化TR
		if ("tr".equalsIgnoreCase(qName) == true) {
			intTdCount = 0;
			flgTr = true;
		}
		// 统计TR
		if ("td".equalsIgnoreCase(qName) == true) {
			intTdCount++;
		}

		if (qName.equalsIgnoreCase("span")) {
			// 获取Table的Id
			String strId = attributes.getValue("id");
			if ("ZBGGList1_Tbl_content".equalsIgnoreCase(strId) == true) {
				flgZBGGList1_Tbl_contentTable = true;
				listZhaoBiaoGG = new ArrayList<ZhaoBiaoGGVO>();
			}
		}
		if (flgZBGGList1_Tbl_contentTable == true
				&& "tr".equalsIgnoreCase(qName) == true) {
			flgTr = true;
			intTdCount = 0;
			itemZhaoBiaoGG = new ZhaoBiaoGGVO();
		}
		if (flgZBGGList1_Tbl_contentTable == true && flgTr == true) {
			// 获取有效的A标签的内容
			if ("a".equalsIgnoreCase(qName) == true) {
				itemZhaoBiaoGG.setLink(attributes.getValue("href"));
				itemZhaoBiaoGG.setTitile(attributes.getValue("title"));
			}
			// 表示第三列的内容
			if (intTdCount == 3) {
				if ("font".equalsIgnoreCase(qName) == true) {
					strTagName = "font";
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if ("tr".equalsIgnoreCase(qName) == true) {
			if (flgZBGGList1_Tbl_contentTable == true) {
				listZhaoBiaoGG.add(itemZhaoBiaoGG);
			}
			// 如果是标签的话,处理结果
			flgTr = false;
		}
		if (flgZBGGList1_Tbl_contentTable == true
				&& "table".equalsIgnoreCase(qName) == true) {
			flgZBGGList1_Tbl_contentTable = false;
		}
	}

	// 调用多次
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (StringUtils.isBlank(this.strTagName) == false) {
			String date = new String(ch, start, length);
			if (this.strTagName.equals("font")
					&& flgZBGGList1_Tbl_contentTable == true) {
				itemZhaoBiaoGG.setDate(date);
				this.strTagName = null;
			}
		}
	}

	public List<ZhaoBiaoGGVO> getListZhaoBiaoGG() {
		return listZhaoBiaoGG;
	}
}
